//
//  UnitImageTextView.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/6/25.
//

import UIKit
import SnapKit

enum UnityImageLayout {
  case leading
  case center
  case trailing
}

class UnitImageTextView: UIView {
  private let imageView = UIImageView(frame: .zero)
  private let label = UILabel(frame: .zero)
  private lazy var stackView = UIStackView(arrangedSubviews: [imageView, label])

  var image: UIImage? {
    didSet {
      imageView.image = image
    }
  }

  var text: String = "" {
    didSet {
      label.text = text
    }
  }

  var font: UIFont = .designKit.bodyBold {
    didSet {
      label.font = font
    }
  }

  var layout: UnityImageLayout = .center {
    didSet {
      switch layout {
      case .center:
        stackView.snp.remakeConstraints { make in
          make.center.equalToSuperview()
        }
      case .leading:
        stackView.snp.remakeConstraints { make in
          make.centerY.equalToSuperview()
          make.left.equalToSuperview().offset(16)
        }
      case .trailing:
        stackView.snp.remakeConstraints { make in
          make.centerY.equalToSuperview()
          make.right.equalToSuperview().offset(-16)
        }
      }
    }
  }

  override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = .white

    layer.cornerRadius = 8
    layer.masksToBounds = true

    label.font = .designKit.bodyBold

    stackView.axis = .horizontal
    stackView.spacing = 4
    addSubview(stackView)

    stackView.snp.makeConstraints { make in
      make.center.equalToSuperview()
    }
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}
